package edu.java基础;

//for循环 当明确循环次数时使用
public class I_循环结构for {
    public static void main(String[] args) {
        int sum = 0;

        //for循环内部定义的变量在循环结束后会从内存中消失

        for (int i = 1; i <= 5; i++){
            System.out.println("第"+i+"次相加");
            sum += i;
            System.out.println(sum);
        }
        //求1-100的偶数和
        int sum1 = 0;
        for (int i = 1; i <= 100; i++){
            if (i % 2 == 0){
                sum1 += i;
            }
        }
        System.out.println("和为"+sum1);

        //水仙花数：三位数，个位，十位，百位的数字立方和等于原数
        //输出所有水仙花数
        for (int i = 100; i<=999; i++){
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;
            if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
                //java幂运算Math.pow(a,b)  返回的是a的b次方
                System.out.println(i+"是水仙花数");
            }
        }

        //每行打印两个水仙花数
        //System.out.print() 打印后不换行 System.out.println() 打印后换行
        int count = 0;  //把count统计变量定义到循环外
        for (int i = 100; i<=999; i++){
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;

            if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
                //java幂运算Math.pow(a,b)  返回的是a的b次方
                System.out.print(i+" ");
                count++;
                if (count %2 ==0){
                    System.out.println();
                }

            }
        }

    }
}
